package com.castle.fortress.admin.orders.service.impl;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.orders.entity.SaleCouponEntity;
import com.castle.fortress.admin.orders.dto.SaleCouponDto;
import com.castle.fortress.admin.orders.mapper.SaleCouponMapper;
import com.castle.fortress.admin.orders.service.SaleCouponService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;

/**
 * 优惠券 服务实现类
 *
 * @author whc
 * @since 2023-01-05
 */
@Service
public class SaleCouponServiceImpl extends ServiceImpl<SaleCouponMapper, SaleCouponEntity> implements SaleCouponService {
    /**
     * 获取查询条件
     *
     * @param saleCouponDto
     * @return
     */
    public QueryWrapper<SaleCouponEntity> getWrapper(SaleCouponDto saleCouponDto) {
        QueryWrapper<SaleCouponEntity> wrapper = new QueryWrapper();
        if (saleCouponDto != null) {
            SaleCouponEntity saleCouponEntity = ConvertUtil.transformObj(saleCouponDto, SaleCouponEntity.class);
            wrapper.like(StrUtil.isNotEmpty(saleCouponEntity.getCouponName()), "coupon_name", saleCouponEntity.getCouponName());
            wrapper.eq(saleCouponEntity.getGetType() != null, "get_type", saleCouponEntity.getGetType());
            wrapper.eq(saleCouponEntity.getUseType() != null, "use_type", saleCouponEntity.getUseType());
            wrapper.eq(saleCouponEntity.getReturnType() != null, "return_type", saleCouponEntity.getReturnType());
            wrapper.eq(saleCouponEntity.getTimeLimit() != null, "time_limit", saleCouponEntity.getTimeLimit());
            wrapper.ge(saleCouponEntity.getTimeStart() != null, "time_start", saleCouponEntity.getTimeStart());
            wrapper.le(saleCouponEntity.getTimeEnd() != null, "time_end", saleCouponEntity.getTimeEnd());
            wrapper.eq(saleCouponEntity.getBackType() != null, "back_type", saleCouponEntity.getBackType());
            wrapper.eq(StrUtil.isNotEmpty(saleCouponEntity.getDeduct()), "deduct", saleCouponEntity.getDeduct());
            wrapper.eq(StrUtil.isNotEmpty(saleCouponEntity.getDiscount()), "discount", saleCouponEntity.getDiscount());
            wrapper.like(StrUtil.isNotEmpty(saleCouponEntity.getDesc()), "desc", saleCouponEntity.getDesc());
            wrapper.eq(saleCouponEntity.getLimitGoodType() != null, "limit_good_type", saleCouponEntity.getLimitGoodType());
            wrapper.eq(saleCouponEntity.getLimitGoodCateType() != null, "limit_good_cate_type", saleCouponEntity.getLimitGoodCateType());
            wrapper.eq(saleCouponEntity.getIsLimitLevel() != null, "is_limit_level", saleCouponEntity.getIsLimitLevel());
            wrapper.eq(saleCouponEntity.getStatus() != null, "status", saleCouponEntity.getStatus());
            wrapper.orderByDesc("create_time");
        }
        return wrapper;
    }


    @Override
    public IPage<SaleCouponDto> pageSaleCoupon(Page<SaleCouponDto> page, SaleCouponDto saleCouponDto) {
        QueryWrapper<SaleCouponEntity> wrapper = getWrapper(saleCouponDto);
        Page<SaleCouponEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<SaleCouponEntity> saleCouponPage = baseMapper.selectPage(pageEntity, wrapper);
        Page<SaleCouponDto> pageDto = new Page(saleCouponPage.getCurrent(), saleCouponPage.getSize(), saleCouponPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(saleCouponPage.getRecords(), SaleCouponDto.class));
        return pageDto;
    }

    @Override
    public IPage<SaleCouponDto> pageSaleCouponExtends(Page<SaleCouponDto> page, SaleCouponDto saleCouponDto) {
        Map<String, Long> pageMap = BizCommonUtil.getPageParam(page);
        SaleCouponEntity saleCouponEntity = ConvertUtil.transformObj(saleCouponDto, SaleCouponEntity.class);
        List<SaleCouponEntity> saleCouponList = baseMapper.extendsList(pageMap, saleCouponEntity);
        Long total = baseMapper.extendsCount(saleCouponEntity);
        Page<SaleCouponDto> pageDto = new Page(page.getCurrent(), page.getSize(), total);
        pageDto.setRecords(ConvertUtil.transformObjList(saleCouponList, SaleCouponDto.class));
        return pageDto;
    }

    @Override
    public SaleCouponDto getByIdExtends(Long id) {
        SaleCouponEntity saleCouponEntity = baseMapper.getByIdExtends(id);
        return ConvertUtil.transformObj(saleCouponEntity, SaleCouponDto.class);
    }

    @Override
    public List<SaleCouponDto> listSaleCoupon(SaleCouponDto saleCouponDto) {
        QueryWrapper<SaleCouponEntity> wrapper = getWrapper(saleCouponDto);
        List<SaleCouponEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list, SaleCouponDto.class);
    }

    @Override
    public List<SaleCouponDto> getMemberAvailCoupon(Long levelId) {
        List<SaleCouponEntity> list =  baseMapper.getMemberAvailCoupon(levelId);
        return ConvertUtil.transformObjList(list, SaleCouponDto.class);
    }
}

